<template>
  <a-card :bordered="false" style="margin-left: 5px">
    <!-- 操作按钮区域 -->
    <div>
      <a-table
        ref="table"
        size="small"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        @change="handleTableChange"
      >
      </a-table>
    </div>
    <!-- table区域-end -->
    <!-- 表单区域 -->
    <!--    <user-modal ref="modalForm" @ok="modalFormOk"></user-modal>-->
    <!--    <Select-User-Modal ref="selectUserModal" @selectFinished="selectOK"></Select-User-Modal>-->
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { deleteAction, getAction, getFileAccessHttpUrl, postAction, putAction } from '@/api/manage'
import ImagCropper from '@views/jeecg/ImagCropper'
import JVxeImageCell from '@comp/JVxeCells/JVxeImageCell'
import ImagPreview from '@views/jeecg/ImagPreview'
import ImgDragSort from '@views/jeecg/ImgDragSort'
import ImgTurnPage from '@views/jeecg/ImgTurnPage'
import { searchByKeywords } from '@api/api'
import { filterObj } from '@/utils/util'
export default {
  name: 'CardUserInfoList',
  mixins: [JeecgListMixin],
  components: {
    ImgTurnPage,
    ImgDragSort,
    ImagPreview,
    JVxeImageCell,
    ImagCropper,
  },
  data() {
    return {
      //搜索框的值
      searInput: '',
      cardNo: '', //发卡的卡号
      isdamage: '', //旧卡状态
      img_url: window._CONFIG['imgDomainURL'] + '/',
      huanka_cardNo: '', //换卡的卡号
      photo: '',
      keys: [],
      description: '用户信息',
      currentUserId: '',
      currentDeptId: '',
      inDepartmentIds:[],
      currentUserCardStatus: 'none',
      faka_: false,
      huanka_: false,
      buka_: false,
      // 表头
      columns: [
        {
          title: '归属部门',
          align: 'center',
          dataIndex: 'department.departName',
        },
        {
          title: '工号',
          align: 'center',
          dataIndex: 'workNo',
        },
        {
          title: '姓名',
          align: 'center',
          dataIndex: 'realname',
        },
        {
          title: '就餐分组',
          align: 'center',
          dataIndex: 'mealTypeTag.name',
        },
        {
          title: '性别',
          align: 'center',
          dataIndex: 'sex',
        },
        {
          title: '职务',
          align: 'center',
          dataIndex: 'post',
        },
        {
          title: '电话',
          align: 'center',
          dataIndex: 'phone',
        },
        {
          title: '卡号',
          align: 'center',
          dataIndex: 'cardNo',
        },
        {
          title: '操作',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
          align: 'center',
          width: 120,
        },
      ],
      url: {
        list: '/admin/org/user/query',
        edit: '/sys/user/editSysDepartWithUser',
        delete: '/sys/user/deleteUserInDepart',
        deleteBatch: '/sys/user/deleteUserInDepartBatch',
        faka: '/admin/org/card/issue',
        guashi: '/admin/org/card/lose',
        tuika: '/admin/org/card/recover',
        huanka: '/admin/org/card/change',
        jiegua: '/admin/org/card/cancel_lose',
        buka: '/admin/org/card/issue',
        importCardsExcelUrl: '/admin/org/card/import',
        importUserCardsExcelUrl: '/admin/org/card/user/import',
      },
    }
  },
  created() {
    this.searchQuery()
  },
  computed: {
    importCardsExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importCardsExcelUrl}`
    },
    importUserCardsExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importUserCardsExcelUrl}`
    },
  },
  methods: {
    getAvatarView: function (avatar) {
      return getFileAccessHttpUrl(avatar)
    },
    searchReset() {
      this.queryParam = {
        departmentId: this.currentDeptId,
        cardStatus: this.currentUserCardStatus,
        includeSubDepartment: true,
      }
      this.loadData(1)
    },

    searchQuery() {
      let params = this.getQueryParams() //查询条件
      // alert('查询条件：部门' + params.departmentId + ' 在职状态' + params.jobStatus)
      if (this.searInput != '' && this.searInput != undefined) {
        params.searchText = this.searInput
        console.log(params.searchText)
      }
      if (params.cardStatus == 'all') {
        params.cardStatus = null
      }
      params.inDepartmentIds =  this.inDepartmentIds
      console.log('所传部门id',this.inDepartmentIds)
      //接收参数 获取list数据
      getAction(this.url.list, params).then((res) => {
        if (res.success && res.result) {
          // this.$message.success(res.message)
          console.log('查询成功:', res.result.records)
          this.dataSource = res.result.records
          this.ipagination.total = res.result.total
        } else {
          this.$message.warning(res.message)
        }
        //  1 2转换 为 男 女
        for (let i = 0; i < this.dataSource.length; i++) {
          let element = this.dataSource[i]
          if (element.sex == '1') {
            this.dataSource[i].sex = '男'
          } else if (element.sex == '2') {
            this.dataSource[i].sex = '女'
          } else {
            this.dataSource[i].sex = ''
          }
        }
        // console.log('this.dataSource[i].sex:',this.dataSource[i].sex)
        // 122
      })
    },
    handleTableChange(pagination, filters, sorter) {
      //分页、排序、筛选变化时触发
      //TODO 筛选
      if (Object.keys(sorter).length > 0) {
        this.isorter.column = sorter.field;
        this.isorter.order = "ascend" == sorter.order ? "asc" : "desc"
      }
      this.ipagination = pagination;
      var params = this.getQueryParams() //查询条件
      this.searchQuery();
    },
    open(qo) {
      //console.log(record);
      this.currentDeptId = qo.deptId
      this.currentUserCardStatus = qo.cardStatus
      this.loadData(1)
      console.log(qo)
    },
    clearList() {
      this.currentDeptId = ''
      this.dataSource = []
    },
    hasSelectDept() {
      if (this.currentDeptId == '') {
        this.$message.error('请选择一个部门!')
        return false
      }
      return true
    },
    handleAddUserDepart() {
      if (this.currentDeptId == '') {
        this.$message.error('请选择一个部门!')
      } else {
        this.$refs.selectUserModal.visible = true
      }
    },




    selectOK(data) {
      let params = {}
      params.depId = this.currentDeptId
      params.userIdList = []
      for (var a = 0; a < data.length; a++) {
        params.userIdList.push(data[a])
      }
      console.log(params)
      postAction(this.url.edit, params).then((res) => {
        if (res.success) {
          this.$message.success(res.message)
          this.loadData()
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    batchDel: function () {
      if (!this.url.deleteBatch) {
        this.$message.error('请设置url.deleteBatch属性!')
        return
      }
      if (!this.currentDeptId) {
        this.$message.error('未选中任何部门，无法取消部门与用户的关联!')
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return
      } else {
        var ids = ''
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ','
        }
        var that = this
        console.log(this.currentDeptId)
        this.$confirm({
          title: '确认取消',
          content: '是否取消用户与选中部门的关联?',
          onOk: function () {
            deleteAction(that.url.deleteBatch, { depId: that.currentDeptId, userIds: ids }).then((res) => {
              if (res.success) {
                that.$message.success('删除用户与选中部门关系成功！')
                that.loadData()
                that.onClearSelected()
              } else {
                that.$message.warning(res.message)
              }
            })
          },
        })
      }
    },
  },
}
</script>
<style scoped>
/** Button按钮间距 */
.ant-btn {
  margin-left: 3px;
}
.ant-card {
  margin-left: -30px;
  margin-right: -30px;
}
.table-page-search-wrapper {
  margin-top: -16px;
  margin-bottom: 16px;
}
.blue_button {
  background: rgb(59, 121, 255);
  padding-left: 50px;
  padding-right: 50px;
  color: white;
}
</style>